package com.note.backend.controller.file;

import com.note.backend.pojo.Note;

import com.note.backend.service.file.NoteService;
import com.note.backend.service.impl.utils.UserDetailsImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/notes")
public class NoteController {

    private final NoteService noteService;
    
    // 获取当前登录用户ID的辅助方法
    private Integer getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
            return userDetails.getUser().getId();
        }
        // 如果未获取到用户信息，可以抛出异常或返回默认值
        throw new RuntimeException("用户未登录");
    }

    @PostMapping
    public Note createNote(@RequestBody Map<String, Object> params) {
        String title = (String) params.get("title");
        String content = (String) params.get("content");
        Integer repositoryId = (Integer) params.get("repositoryId");
        Integer userId = getCurrentUserId();

        return noteService.createNote(title, content, repositoryId, userId);
    }

    @PutMapping("/{id}")
    public Note updateNote(@PathVariable Integer id, @RequestBody Map<String, Object> params) {
        String title = (String) params.get("title");
        String content = (String) params.get("content");
        Integer userId = getCurrentUserId();

        return noteService.saveNote(title, content, id, userId);
    }

    @PostMapping("/documents")
    public Note createDocument(@RequestBody Map<String, Object> params) {
        String title = (String) params.get("title");
        Integer noteId = (Integer) params.get("noteId");
        Integer userId = getCurrentUserId();

        return noteService.createDocument(title,noteId,userId);
    }

    @GetMapping
    public List<Note> getNotes(@RequestParam(required = false) Integer repositoryId) {
        Integer userId = getCurrentUserId();

        if (repositoryId != null) {
            return noteService.getNotesByRepositoryId(repositoryId);
        } else {
            return noteService.getNotesByUserId(userId);
        }
    }

    @GetMapping("/{id}")
    public Note getNote(@PathVariable Integer id) {
        return noteService.getNoteById(id);
    }

    @DeleteMapping("/{id}")
    public void deleteNote(@PathVariable Integer id) {
        Integer userId = getCurrentUserId();
        noteService.deleteNote(id, userId);
    }

    @PutMapping("/{id}/repository")
    public void addNoteToRepository(@PathVariable Integer id, @RequestBody Map<String, Object> params) {
        Integer repositoryId = (Integer) params.get("repositoryId");
        Integer userId = getCurrentUserId();

        noteService.addNoteToRepository(id, repositoryId, userId);
    }

    @DeleteMapping("/{id}/repository")
    public void removeNoteFromRepository(@PathVariable Integer id) {
        Integer userId = getCurrentUserId();
        noteService.removeNoteFromRepository(id, userId);
    }
}